<template>
  <a-modal
    title="编辑業主賬單"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="账单开始时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择账单开始时间" v-decorator="['rentStartDate',{rules: [{ required: true, message: '请选择账单开始时间！' }]}]" @change="onChangerentStartDate"/>
        </a-form-item>
        <a-form-item
          label="账单结束时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择账单结束时间" v-decorator="['rentEndDate',{rules: [{ required: true, message: '请选择账单结束时间！' }]}]" @change="onChangerentEndDate"/>
        </a-form-item>
        <a-form-item
          label="是否免租"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group placeholder="请选择是否免租" v-decorator="['rentFree',{rules: [{ required: true, message: '请选择是否免租！' }]}]" >
            <a-radio v-for="(item,index) in rentFreeData" :key="index" :value="item.code">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="租金调整"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group placeholder="请选择租金调整" v-decorator="['rentAdjust',{rules: [{ required: true, message: '请选择租金调整！' }]}]" >
            <a-radio v-for="(item,index) in rentAdjustData" :key="index" :value="item.code">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { ownerLeaseBillEdit } from '@/api/modular/main/ownerleasebill/ownerLeaseBillManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        rentStartDateDateString: '',
        rentEndDateDateString: '',
        rentFreeData: [],
        rentAdjustData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        const collectionTypeOption = this.$options
        this.collectionTypeData = collectionTypeOption.filters['dictData']('common_status')
        const rentFreeOption = this.$options
        this.rentFreeData = rentFreeOption.filters['dictData']('common_status')
        const rentAdjustOption = this.$options
        this.rentAdjustData = rentAdjustOption.filters['dictData']('common_status')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              rentFree: record.rentFree,
              rentAdjust: record.rentAdjust
            }
          )
        }, 100)
        // 时间单独处理
        if (record.rentStartDate != null) {
            this.form.getFieldDecorator('rentStartDate', { initialValue: moment(record.rentStartDate, 'YYYY-MM-DD') })
        }
        this.rentStartDateDateString = moment(record.rentStartDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentEndDate != null) {
            this.form.getFieldDecorator('rentEndDate', { initialValue: moment(record.rentEndDate, 'YYYY-MM-DD') })
        }
        this.rentEndDateDateString = moment(record.rentEndDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.rentStartDate = this.rentStartDateDateString
            values.rentEndDate = this.rentEndDateDateString
            ownerLeaseBillEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangerentStartDate(date, dateString) {
        this.rentStartDateDateString = dateString
      },
      onChangerentEndDate(date, dateString) {
        this.rentEndDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
